import minecraft_launcher_lib
import os

class instanceManager:

    # 欢迎语函数
    def welcomeTitle(self):
        print("  * ================================================================ *")
        print("  |                      LitsManager(Instance)                       |")
        print("  |     _     _ _       __  __                                       |")
        print("  |    | |   (_) |_ ___|  \/  | __ _ _ __   __ _  __ _  ___ _ __     |")
        print("  |    | |   | | __/ __| |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '__|    |")
        print("  |    | |___| | |_\__ \ |  | | (_| | | | | (_| | (_| |  __/ |       |")
        print("  |    |_____|_|\__|___/_|  |_|\__,_|_| |_|\__,_|\__, |\___|_|       |")
        print("  |                                              |___/               |")
        print("  |             *-------------- Info --------------*                 |")
        print("  |                       Version B2020831                           |")
        print("  |                 Powered By NodeSheep Network                     |")
        print("  |                     Made By LittleSheep_                         |")
        print("  |            Mirror original settings, sir (But not made)          |")
        print("  |                                                                  |")
        print("  + ------------------- Press Enter key continue ------------------- *")
        input("  * --> ")

        os.system("clear")

    def coreUI(self) -> bool:
        while True:
            print("  * --------------------------- Manager ---------------------------- *")
            print("  |  1. Create instance                                              |")
            print("  |  2. Delete instance                                              |")
            print("  |  3. Show instance list                                           |")
            print("  |  Q. Back                                                         |")
            print("  * --------------------------- Selector --------------------------- *")
            selectorGet = input("  * -->")

            os.system("clear")

            # 返回函数
            if selectorGet == "q" or selectorGet == "Q":
                break

            # 创建/下载实例
            if selectorGet == "1":
                print("  * --------------------------- Creator ---------------------------- *")
                print("  * $ Now get minecraft lastest versions... ")

                # 获取最新版
                latestMinecraftRelease = minecraft_launcher_lib.utils.get_latest_version()["release"]
                latestMinecraftSnapshot = minecraft_launcher_lib.utils.get_latest_version()["snapshot"]

                print("  * $ Now get minecraft other versions... ")

                # 获取其他版本列表
                allMinecraftVersionList = minecraft_launcher_lib.utils.get_version_list()

                print("  * $ Now process to list... ")
                print("  * ------- End Log")
                print("  * $ Now start print (^○^) !")
                print("  * ------- Version List")

                # 打印
                # 判断没有最新版 (快照)
                if latestMinecraftSnapshot == latestMinecraftRelease:
                    print(f"  * ------- Release")
                    print(f"  * $ Name({latestMinecraftRelease})")
                    print(f"  * ------- Snapshot")
                    print(f"  * $ Have not snapshot... ")
                    print(f"  * ------- End Print")

                elif latestMinecraftRelease is not None and latestMinecraftSnapshot is not None:
                    print(f"  * ------- Release")
                    print(f"  * $ Name({latestMinecraftRelease})")
                    print(f"  * ------- Snapshot")
                    print(f"  * $ Name({latestMinecraftSnapshot}) ")
                    print(f"  * ------- End Print")

                else:
                    print(f"  * ------- Error")
                    print(f"  * $ Cannot get version info.")
                    print(f"  * $ Please check your network.")
                    print(f"  * ------- End Print")
                    return False

                # Downloader 处理
                print("  * -------------------------- Downloader -------------------------- *")
                print("  * $ Tips: ")
                print("  * $ Enter version name to install minecraft | Enter Null string quit")
                print("  * $ Enter VERSIONS to show versions list    | Need three parameter")
                print("  * $ Enter other auto quit                   ｜Enter Q quit")
                downloaderGet = input("  * & -- Please Enter (#^.^#) --> ").lower()

                # 创建认证变量
                getInstallMode = False

                # 处理 Version 命令
                if downloaderGet == "versions":
                    pass

                # 处理普通下载模式
                else:

                    # 判断是否输入的是版本号
                    for i in range(len(allMinecraftVersionList)):
                        # 找到了
                        if allMinecraftVersionList[i]["id"] == downloaderGet: getInstallMode = True; break
                        # 没有找到
                        else: getInstallMode = False; break

                    # 判断认证变量存在并且正常
                    if getInstallMode:

                        # 删除认证变量
                        del getInstallMode

        return True